using System;
using System.Collections.Generic;
using System.Text;
using System.Resources;
using System.IO;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;
using SQLString.Dialogs;
using SQLString.AppResource;

namespace SQLString.Classes
{
	class SQLObjectManager
	{
		SQLResource AppRes = null;
		string Connection = string.Empty;
		public SQLObjectManager( SQLResource res,string ODBCConnect )
		{
			if (res != null && string.IsNullOrEmpty(ODBCConnect)!=true)
			{
				AppRes = res;
				Connection = ODBCConnect.Trim();
			}
		}

		//	Load Language, Data Type, Framework

		///	*********************************************************************************************************
		///	*	Project:		SQLString Project
		///	*	Copyright: 	(C) jbhSOFTsys
		///	*	Developer: 		jeffHamlin
		///	*********************************************************************************************************
		///
		///	<summary>
		/// 
		///	DESCRIPTION/DATE ADDED:
		///	-----------------------
		///		DESCRIPTION:		Loads the languages;
		///		CREATED:			07.05.08;
		///
		///	REQUIRED FUNCTION PARAMETERS:	(Default DB Connect String Values for Miscoroft SQL Server)
		///	-----------------------------
		///		Function Name:		LoadCombos;
		///		Scope:				public;
		///		ListBox:			lstcnt;
		///		SortedList			slLang;
		///		DB Connect:			Connection;	
		///		SQL Parameter:		"SELECT LANG_ID, LANG_NM FROM T_LANG";
		///	</summary>
		///
		///	<code>
		/// 
		///	</code>
		///
		///	*********************************************************************************************************

		//	
		//
		public System.Collections.SortedList LoadCombos( System.Windows.Forms.ComboBox lstcnt, string szSQLNeeded)
		{
			String szConnectValue = string.Empty;
			System.Collections.SortedList slLang=new System.Collections.SortedList();
			szConnectValue = Connection;
			if (String.IsNullOrEmpty(Connection)==false)
			{
				//	Initialize the database connection, if null or fail return
				System.Data.SqlClient.SqlConnection dbConn = new System.Data.SqlClient.SqlConnection(Connection);
				if (dbConn != null)
				{
					if (lstcnt.Items.Count > 0)
					{
						lstcnt.Items.Clear();
					}
					if (slLang.Count > 0)
					{
						slLang.Clear();
					}
					try
					{
						String szSQL = string.Empty;
						szSQL = szSQLNeeded.Trim();
						if (dbConn != null)
						{
							System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(szSQL);
							sqlCmd.Connection = dbConn;
							dbConn.Open();	//	Open the connection to the database
							//	Checking database connection to see if it closed for some reason
							//	where I work...this happens a lot
							if (dbConn.State == System.Data.ConnectionState.Open)
							{
								SqlDataReader sqlRdr = sqlCmd.ExecuteReader(CommandBehavior.Default);		//	Execute the sql
								while (sqlRdr.Read())
								{
									if (sqlRdr.HasRows)
									{

										lstcnt.Items.Add(( sqlRdr.IsDBNull(0) == false ) ? sqlRdr.GetString(0).Trim() : string.Empty);
										slLang.Add(sqlRdr.GetInt16(1), ( sqlRdr.IsDBNull(0) == false ) ? sqlRdr.GetString(0).Trim() : string.Empty);
									}
								}
								{
									//	Cleanup and get out
									if (sqlRdr.IsClosed == false)
									{
										sqlRdr.Close();
									}
									sqlCmd.Dispose();
									return slLang;		//send the collection of data
								}

							}
							else
								return slLang;

						}
						else
							return slLang;
					}
					catch (System.Data.SqlClient.SqlException ex)
					{
						System.Windows.Forms.MessageBox.Show("SQL Error: " + ex.Message, "Error",
							System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
						return slLang;	//	or false
					}
					catch (Exception ex)
					{
						System.Windows.Forms.MessageBox.Show("System Error: " + ex.Message, "Error",
							System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
						return slLang;	//	or false
					}
					finally
					{
						dbConn.Dispose();
					}
				}
				else
					return slLang;
			}
			else
				return slLang;
		}

		
	}
}
